﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace QLNHK.BLL
{
	public partial class NhanKhau_QuanHeGiaDinh : Ent_Base, ICloneable
	{
        #region Variables
        
		private int _iD;
		private string _hoTen = string.Empty;
		private DateTime _ngaySinh = new DateTime(1753, 1, 1);
		private int _gioiTinh;
		private int _quanHeID;
		private string _ngheNghiep = string.Empty;
		private string _diaChiNoiOHienNay = string.Empty;
		private long _nhanKhauID;
        
        #endregion
    
        //---------------------------------------------------------------------------------------------
        
		#region Properties.
		
		public int ID 
        { 
            get { return  _iD; } 
            set
            {
                _iD = value;
                OnPropertyChanged("ID");
            }
        }
		public string HoTen 
        { 
            get { return  _hoTen; } 
            set
            {
                _hoTen = value;
                OnPropertyChanged("HoTen");
            }
        }
		public DateTime NgaySinh 
        { 
            get { return  _ngaySinh; } 
            set
            {
                _ngaySinh = value;
                OnPropertyChanged("NgaySinh");
            }
        }
		public int GioiTinh 
        { 
            get { return  _gioiTinh; } 
            set
            {
                _gioiTinh = value;
                OnPropertyChanged("GioiTinh");
            }
        }
		public int QuanHeID 
        { 
            get { return  _quanHeID; } 
            set
            {
                _quanHeID = value;
                OnPropertyChanged("QuanHeID");
            }
        }
		public string NgheNghiep 
        { 
            get { return  _ngheNghiep; } 
            set
            {
                _ngheNghiep = value;
                OnPropertyChanged("NgheNghiep");
            }
        }
		public string DiaChiNoiOHienNay 
        { 
            get { return  _diaChiNoiOHienNay; } 
            set
            {
                _diaChiNoiOHienNay = value;
                OnPropertyChanged("DiaChiNoiOHienNay");
            }
        }
		public long NhanKhauID 
        { 
            get { return  _nhanKhauID; } 
            set
            {
                _nhanKhauID = value;
                OnPropertyChanged("NhanKhauID");
            }
        }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Methods
		protected static List<NhanKhau_QuanHeGiaDinh> ConvertToCollection(IDataReader reader)
		{
			List<NhanKhau_QuanHeGiaDinh> collection = new List<NhanKhau_QuanHeGiaDinh>();
			while (reader.Read())
			{
				NhanKhau_QuanHeGiaDinh entity = new NhanKhau_QuanHeGiaDinh();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoTen"))) entity.HoTen = reader.GetString(reader.GetOrdinal("HoTen"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgaySinh"))) entity.NgaySinh = reader.GetDateTime(reader.GetOrdinal("NgaySinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("GioiTinh"))) entity.GioiTinh = reader.GetInt32(reader.GetOrdinal("GioiTinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("QuanHeID"))) entity.QuanHeID = reader.GetInt32(reader.GetOrdinal("QuanHeID"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgheNghiep"))) entity.NgheNghiep = reader.GetString(reader.GetOrdinal("NgheNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiaChiNoiOHienNay"))) entity.DiaChiNoiOHienNay = reader.GetString(reader.GetOrdinal("DiaChiNoiOHienNay"));
				if (!reader.IsDBNull(reader.GetOrdinal("NhanKhauID"))) entity.NhanKhauID = reader.GetInt64(reader.GetOrdinal("NhanKhauID"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;
		}
		
		public static bool Find(List<NhanKhau_QuanHeGiaDinh> collection, int iD)
        {
            foreach (NhanKhau_QuanHeGiaDinh item in collection)
            {
                if (item.ID == iD)
                {
                    return true;
                }
            }

            return false;
        }
		
		public static void UpdateDataSet(DataSet ds)
        {
            string insert = "Insert INTO t_NhanKhau_QuanHeGiaDinh VALUES(@HoTen, @NgaySinh, @GioiTinh, @QuanHeID, @NgheNghiep, @DiaChiNoiOHienNay, @NhanKhauID)";
            string update = "UPDATE t_NhanKhau_QuanHeGiaDinh SET HoTen = @HoTen, NgaySinh = @NgaySinh, GioiTinh = @GioiTinh, QuanHeID = @QuanHeID, NgheNghiep = @NgheNghiep, DiaChiNoiOHienNay = @DiaChiNoiOHienNay, NhanKhauID = @NhanKhauID WHERE ID = @ID";
            string delete = "DELETE FROM t_NhanKhau_QuanHeGiaDinh WHERE ID = @ID";

			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();

            System.Data.Common.DbCommand InsertCommand = db.GetSqlStringCommand(insert);
			db.AddInParameter(InsertCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@HoTen", SqlDbType.NVarChar, "HoTen", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgaySinh", SqlDbType.DateTime, "NgaySinh", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@GioiTinh", SqlDbType.Int, "GioiTinh", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@QuanHeID", SqlDbType.Int, "QuanHeID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgheNghiep", SqlDbType.NVarChar, "NgheNghiep", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@DiaChiNoiOHienNay", SqlDbType.NVarChar, "DiaChiNoiOHienNay", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NhanKhauID", SqlDbType.BigInt, "NhanKhauID", DataRowVersion.Current);

            System.Data.Common.DbCommand UpdateCommand = db.GetSqlStringCommand(update);
			db.AddInParameter(UpdateCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@HoTen", SqlDbType.NVarChar, "HoTen", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgaySinh", SqlDbType.DateTime, "NgaySinh", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@GioiTinh", SqlDbType.Int, "GioiTinh", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@QuanHeID", SqlDbType.Int, "QuanHeID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgheNghiep", SqlDbType.NVarChar, "NgheNghiep", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@DiaChiNoiOHienNay", SqlDbType.NVarChar, "DiaChiNoiOHienNay", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NhanKhauID", SqlDbType.BigInt, "NhanKhauID", DataRowVersion.Current);
			
            System.Data.Common.DbCommand DeleteCommand = db.GetSqlStringCommand(delete);
			db.AddInParameter(DeleteCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);

            db.UpdateDataSet(ds, ds.Tables[0].TableName, InsertCommand, UpdateCommand, DeleteCommand, UpdateBehavior.Standard);
        }
				
		public static void UpdateDataSet(DataSet ds, string tableName)
        {
            string insert = "Insert INTO t_NhanKhau_QuanHeGiaDinh VALUES(@HoTen, @NgaySinh, @GioiTinh, @QuanHeID, @NgheNghiep, @DiaChiNoiOHienNay, @NhanKhauID)";
            string update = "UPDATE t_NhanKhau_QuanHeGiaDinh SET HoTen = @HoTen, NgaySinh = @NgaySinh, GioiTinh = @GioiTinh, QuanHeID = @QuanHeID, NgheNghiep = @NgheNghiep, DiaChiNoiOHienNay = @DiaChiNoiOHienNay, NhanKhauID = @NhanKhauID WHERE ID = @ID";
            string delete = "DELETE FROM t_NhanKhau_QuanHeGiaDinh WHERE ID = @ID";

			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();

            System.Data.Common.DbCommand InsertCommand = db.GetSqlStringCommand(insert);
			db.AddInParameter(InsertCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@HoTen", SqlDbType.NVarChar, "HoTen", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgaySinh", SqlDbType.DateTime, "NgaySinh", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@GioiTinh", SqlDbType.Int, "GioiTinh", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@QuanHeID", SqlDbType.Int, "QuanHeID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgheNghiep", SqlDbType.NVarChar, "NgheNghiep", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@DiaChiNoiOHienNay", SqlDbType.NVarChar, "DiaChiNoiOHienNay", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NhanKhauID", SqlDbType.BigInt, "NhanKhauID", DataRowVersion.Current);

            System.Data.Common.DbCommand UpdateCommand = db.GetSqlStringCommand(update);
			db.AddInParameter(UpdateCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@HoTen", SqlDbType.NVarChar, "HoTen", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgaySinh", SqlDbType.DateTime, "NgaySinh", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@GioiTinh", SqlDbType.Int, "GioiTinh", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@QuanHeID", SqlDbType.Int, "QuanHeID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgheNghiep", SqlDbType.NVarChar, "NgheNghiep", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@DiaChiNoiOHienNay", SqlDbType.NVarChar, "DiaChiNoiOHienNay", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NhanKhauID", SqlDbType.BigInt, "NhanKhauID", DataRowVersion.Current);
			
            System.Data.Common.DbCommand DeleteCommand = db.GetSqlStringCommand(delete);
			db.AddInParameter(DeleteCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);

            db.UpdateDataSet(ds, tableName, InsertCommand, UpdateCommand, DeleteCommand, UpdateBehavior.Standard);
        }

		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static NhanKhau_QuanHeGiaDinh Load(int iD)
		{
			const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_Load.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            IDataReader reader = db.ExecuteReader(dbCommand);
			List<NhanKhau_QuanHeGiaDinh> collection = ConvertToCollection(reader);	
			if (collection.Count > 0)
			{
				return collection[0];
			}
			return null;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static List<NhanKhau_QuanHeGiaDinh> SelectCollectionAll()
		{
			IDataReader reader = SelectReaderAll();
			return ConvertToCollection(reader);			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static List<NhanKhau_QuanHeGiaDinh> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			return ConvertToCollection(reader);		
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static List<NhanKhau_QuanHeGiaDinh> SelectCollectionBy_QuanHeID(int quanHeID)
		{
            IDataReader reader = SelectReaderBy_QuanHeID(quanHeID);
			return ConvertToCollection(reader);	
		}		
		//---------------------------------------------------------------------------------------------
		public static List<NhanKhau_QuanHeGiaDinh> SelectCollectionBy_NhanKhauID(long nhanKhauID)
		{
            IDataReader reader = SelectReaderBy_NhanKhauID(nhanKhauID);
			return ConvertToCollection(reader);	
		}		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectBy_QuanHeID(int quanHeID)
		{
			const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_SelectBy_QuanHeID.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@QuanHeID", SqlDbType.Int, quanHeID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------
		public static DataSet SelectBy_NhanKhauID(long nhanKhauID)
		{
			const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_SelectBy_NhanKhauID.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, nhanKhauID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_SelectAll.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_SelectDynamic.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_SelectAll.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_SelectDynamic.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IDataReader SelectReaderBy_QuanHeID(int quanHeID)
		{
			const string spName = "p_NhanKhau_QuanHeGiaDinh_SelectBy_QuanHeID.generated";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@QuanHeID", SqlDbType.Int, quanHeID);
			
            return db.ExecuteReader(dbCommand);
		}		
		//---------------------------------------------------------------------------------------------
		public static IDataReader SelectReaderBy_NhanKhauID(long nhanKhauID)
		{
			const string spName = "p_NhanKhau_QuanHeGiaDinh_SelectBy_NhanKhauID.generated";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, nhanKhauID);
			
            return db.ExecuteReader(dbCommand);
		}		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertNhanKhau_QuanHeGiaDinh(string hoTen, DateTime ngaySinh, int gioiTinh, int quanHeID, string ngheNghiep, string diaChiNoiOHienNay, long nhanKhauID)
		{
			NhanKhau_QuanHeGiaDinh entity = new NhanKhau_QuanHeGiaDinh();	
			entity.HoTen = hoTen;
			entity.NgaySinh = ngaySinh;
			entity.GioiTinh = gioiTinh;
			entity.QuanHeID = quanHeID;
			entity.NgheNghiep = ngheNghiep;
			entity.DiaChiNoiOHienNay = diaChiNoiOHienNay;
			entity.NhanKhauID = nhanKhauID;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_Insert.generated]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
			db.AddInParameter(dbCommand, "@HoTen", SqlDbType.NVarChar, HoTen);
			db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year <= 1753 ? DBNull.Value : (object) NgaySinh);
			db.AddInParameter(dbCommand, "@GioiTinh", SqlDbType.Int, GioiTinh);
			db.AddInParameter(dbCommand, "@QuanHeID", SqlDbType.Int, QuanHeID);
			db.AddInParameter(dbCommand, "@NgheNghiep", SqlDbType.NVarChar, NgheNghiep);
			db.AddInParameter(dbCommand, "@DiaChiNoiOHienNay", SqlDbType.NVarChar, DiaChiNoiOHienNay);
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, NhanKhauID);
			
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				ID = (int) db.GetParameterValue(dbCommand, "@ID");
				return ID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				ID = (int) db.GetParameterValue(dbCommand, "@ID");
				return ID;
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(List<NhanKhau_QuanHeGiaDinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (NhanKhau_QuanHeGiaDinh item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateNhanKhau_QuanHeGiaDinh(int iD, string hoTen, DateTime ngaySinh, int gioiTinh, int quanHeID, string ngheNghiep, string diaChiNoiOHienNay, long nhanKhauID)
		{
			NhanKhau_QuanHeGiaDinh entity = new NhanKhau_QuanHeGiaDinh();			
			entity.ID = iD;
			entity.HoTen = hoTen;
			entity.NgaySinh = ngaySinh;
			entity.GioiTinh = gioiTinh;
			entity.QuanHeID = quanHeID;
			entity.NgheNghiep = ngheNghiep;
			entity.DiaChiNoiOHienNay = diaChiNoiOHienNay;
			entity.NhanKhauID = nhanKhauID;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_NhanKhau_QuanHeGiaDinh_InsertUpdate.generated";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			db.AddInParameter(dbCommand, "@HoTen", SqlDbType.NVarChar, HoTen);
			db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year <= 1753 ? DBNull.Value : (object) NgaySinh);
			db.AddInParameter(dbCommand, "@GioiTinh", SqlDbType.Int, GioiTinh);
			db.AddInParameter(dbCommand, "@QuanHeID", SqlDbType.Int, QuanHeID);
			db.AddInParameter(dbCommand, "@NgheNghiep", SqlDbType.NVarChar, NgheNghiep);
			db.AddInParameter(dbCommand, "@DiaChiNoiOHienNay", SqlDbType.NVarChar, DiaChiNoiOHienNay);
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, NhanKhauID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(List<NhanKhau_QuanHeGiaDinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (NhanKhau_QuanHeGiaDinh item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateNhanKhau_QuanHeGiaDinh(int iD, string hoTen, DateTime ngaySinh, int gioiTinh, int quanHeID, string ngheNghiep, string diaChiNoiOHienNay, long nhanKhauID)
		{
			NhanKhau_QuanHeGiaDinh entity = new NhanKhau_QuanHeGiaDinh();			
			entity.ID = iD;
			entity.HoTen = hoTen;
			entity.NgaySinh = ngaySinh;
			entity.GioiTinh = gioiTinh;
			entity.QuanHeID = quanHeID;
			entity.NgheNghiep = ngheNghiep;
			entity.DiaChiNoiOHienNay = diaChiNoiOHienNay;
			entity.NhanKhauID = nhanKhauID;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_Update.generated]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			db.AddInParameter(dbCommand, "@HoTen", SqlDbType.NVarChar, HoTen);
			db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year <= 1753 ? DBNull.Value : (object) NgaySinh);
			db.AddInParameter(dbCommand, "@GioiTinh", SqlDbType.Int, GioiTinh);
			db.AddInParameter(dbCommand, "@QuanHeID", SqlDbType.Int, QuanHeID);
			db.AddInParameter(dbCommand, "@NgheNghiep", SqlDbType.NVarChar, NgheNghiep);
			db.AddInParameter(dbCommand, "@DiaChiNoiOHienNay", SqlDbType.NVarChar, DiaChiNoiOHienNay);
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, NhanKhauID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(List<NhanKhau_QuanHeGiaDinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (NhanKhau_QuanHeGiaDinh item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteNhanKhau_QuanHeGiaDinh(int iD)
		{
			NhanKhau_QuanHeGiaDinh entity = new NhanKhau_QuanHeGiaDinh();
			entity.ID = iD;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_Delete.generated]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static int DeleteBy_QuanHeID(int quanHeID)
		{
			const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_DeleteBy_QuanHeID.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@QuanHeID", SqlDbType.Int, quanHeID);
						
            return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
			
		public static int DeleteBy_NhanKhauID(long nhanKhauID)
		{
			const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_DeleteBy_NhanKhauID.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, nhanKhauID);
						
            return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
			
		
		public static int DeleteDynamic(string whereCondition)
		{
			const string spName = "[dbo].[p_NhanKhau_QuanHeGiaDinh_DeleteDynamic.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            
            return db.ExecuteNonQuery(dbCommand);   
		}
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(List<NhanKhau_QuanHeGiaDinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (NhanKhau_QuanHeGiaDinh item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
		
        //---------------------------------------------------------------------------------------------
		
        #region ICloneable Members

        public object Clone()
        {
            return base.MemberwiseClone();
        }

        public NhanKhau_QuanHeGiaDinh CloneObject()
        {
            return this.Clone() as NhanKhau_QuanHeGiaDinh;
        }
        
        #endregion
	}	
}